﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;

namespace WindowsFormsApplication1
{
    public partial class frmInDiemTongHop : Form
    {
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");

        public frmInDiemTongHop()
        {
            InitializeComponent();
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {

                string sql = "exec PC_TaoViewTongHopDiem N'" + txtTuhocky.Text + "', N'" + txtDenhocky.Text + "', N'" + lku_Malop.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Connection.Open();
                int i= cmd.ExecuteNonQuery();
                
                DataSet ds = FillDataset("exec PC_TongHopDiem '" + 1 + "'");
                gridControl1.DataSource = ds.Tables[0];
                gridView1.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            connection.Close();

        }

        private void frmInDiemTongHop_Load(object sender, EventArgs e)
        {
            DataSet ds = FillDataset("SELECT [MalopID],[Tenlop] FROM [T03_Lop_hoc]");
            lku_Malop.Properties.DataSource = ds.Tables[0];
        }

     

        private void btnExport_Click(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            saveFileDialog1.AddExtension = true;
            saveFileDialog1.Filter = "Microsoft Office Excel Workbook (*.xls)|*.xls";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    DevExpress.XtraExport.ExportXlsProvider provider = new DevExpress.XtraExport.ExportXlsProvider(saveFileDialog1.FileName);
                    DevExpress.XtraGrid.Export.BaseExportLink link = view.CreateExportLink(provider);
                    link.ExportCellsAsDisplayText = false;
                    link.ExportAll = true;
                    link.ExportTo(true);
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = saveFileDialog1.FileName;
                    process.Start();
                    process.WaitForInputIdle();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error " + ex.Message);
                }
            }
        }
    }
}
